<template>
  <el-dialog
    @close="close"
    :title="this.formData.id ? '修改学科' : '添加学科'"
    width="20%"
    :visible.sync="dialogVisible"
  >
    <div>
      <el-form
        label-width="80px"
        :model="formData"
        :rules="formRules"
        ref="from"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input
            size="small"
            v-model="formData.subjectName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch
            v-model="formData.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer">
      <el-button size="small" @click="close">取 消</el-button>
      <el-button size="small" type="primary" @click="btnOK">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addsubjectApi, editsubjectApi } from "@/api/subject";
export default {
  data() {
    return {
      dialogVisible: false,
      value: true,
      formData: {
        subjectName: "",
        isFrontDisplay: "1",
        id: "",
      },
      formRules: {
        subjectName: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },

  methods: {
    async btnOK() {
      try {
        await this.$refs.from.validate();
      } catch (error) {
        console.log(error);
      }
      if (this.formData.id) {
        // 修改
        await editsubjectApi(this.formData);
        this.$message.success("修改成功");
      } else {
        // 发起添加请求
        await addsubjectApi(this.formData);
        // 提示
        this.$message.success("添加成功");
      }

      this.dialogVisible = false;
      this.formData = { subjectName: "", isFrontDisplay: "1", id: "" };
      this.$emit("btnK");
    },
    close() {
      this.dialogVisible = false;
      this.formData = {
        subjectName: "",
        isFrontDisplay: "1",
        id: "",
      };
    },
  },
};
</script>

<style lang='less' scoped>
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    border-radius: 8px 8px 0px 0px;
  }
  .el-dialog__close {
    color: #fff;
  }
}
</style>